package com.sw.small_world.fragment.home.persondoing;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sw.small_world.R;
import com.sw.small_world.activity.MainActivity;
import com.sw.small_world.fragment.base.BaseListFragment;
import com.sw.small_world.fragment.home.HomeFragment;
import com.sw.small_world.utils.CommonUtils;
import com.sw.small_world.utils.ConstantUtils;
import com.sw.small_world.utils.StartUtils;
import com.sw.small_world.utils.ToastUtil;
import com.sw.small_world.widgets.CircleImageView;
import com.sw.small_world.widgets.MarkStreamLayout;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/16.
 */

public class PersonDoingFragment extends BaseListFragment<String> {

    private MainActivity activity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (MainActivity) getActivity();
        activity.tvTitle.setText("人脉办事");
    }

    @Override
    protected Object requestData() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                StartUtils.startActivityById(activity, ConstantUtils.DETAIL_PERSON_DOING);
            }
        });
        for (int i = 0; i < 10; i++) {
            list.add("" + i);
        }
        return ConstantUtils.STATE_SUCCESSED;
    }

    @Override
    protected View setView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(activity, R.layout.item_person_doing, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ArrayList<String> list = new ArrayList<>();
        list.add("杀猪的");
        list.add("健康");
        initMark(list,holder.mark);
        return convertView;
    }
    private void initMark(ArrayList<String> list,MarkStreamLayout mark) {
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.rightMargin = CommonUtils.sp2px(5);
        lp.bottomMargin = CommonUtils.sp2px(5);
        for (int i = 0; i < list.size(); i++) {
            TextView view = new TextView(getActivity());
            view.setText(list.get(i));
            view.setTextSize(10);
            view.setTextColor(Color.parseColor("#999999"));
            view.setBackgroundResource(R.drawable.shape_round_grey);
            view.setPadding(CommonUtils.sp2px(10), CommonUtils.sp2px(3),
                    CommonUtils.sp2px(10), CommonUtils.sp2px(3));
            final int finalI = i;
            mark.addView(view, lp);
        }
    }
    @Override
    protected void setRefresh() {
        new Thread() {
            @Override
            public void run() {
                try {
                    sleep(1000);
                    CommonUtils.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            refreshListView.onRefreshComplete();
                            ToastUtil.show("刷新成功");
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                super.run();
            }
        }.start();
    }

    @Override
    protected void loadMore() {
        new Thread() {
            @Override
            public void run() {
                try {
                    sleep(1000);
                    CommonUtils.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            refreshListView.onRefreshComplete();
                            ToastUtil.show("加载更多成功");
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                super.run();
            }
        }.start();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    static class ViewHolder {
        @Bind(R.id.iv_portrait)
        CircleImageView ivPortrait;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_deep)
        TextView tvDeep;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.mark)
        MarkStreamLayout mark;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.tv_see_num)
        TextView tvSeeNum;
        @Bind(R.id.collect)
        RelativeLayout collect;
        @Bind(R.id.grade)
        RelativeLayout grade;
        @Bind(R.id.answer)
        RelativeLayout answer;
        @Bind(R.id.ll2)
        LinearLayout ll2;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}